﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace TUM.Mustertexte_Parser
{
    public class TemplateTopic
    {
        protected XmlNode TopicNode;
        protected string TemplateFilePath;

        /// <summary>
        /// Gets the name of the topic.
        /// </summary>
        public string Name
        {
            get
            {
                if (_Name == null)
                {
                    XmlNode nameNode = TopicNode.SelectSingleNode("Name");
                    _Name = nameNode != null ? nameNode.InnerText : string.Empty;
                }
                return _Name;
            }
        }
        string _Name;


        public TextTemplate[] Templates
        {
            get
            {
                if (_Templates == null)
                    LoadTextTemplates();
                return _Templates;
            }
        }
        TextTemplate[] _Templates;


        public AdditionalProductTopic[] AdditionalTopics
        {
            get
            {
                if (_AdditionalTopics == null)
                    LoadAdditionalTopics();
                return _AdditionalTopics;
            }
        }
        private AdditionalProductTopic[] _AdditionalTopics;

        public object Tag
        {
            get;
            set;
        }


        internal TemplateTopic(string templateFilePath, XmlNode topicNode)
        {
            if (templateFilePath == null)
                throw new ArgumentNullException("templateFilePath");
            if (topicNode == null)
                throw new ArgumentNullException("topicNode");
            TemplateFilePath = templateFilePath;
            TopicNode = topicNode;

            LoadTextTemplates();
        }


        private void LoadTextTemplates()
        {
            var templateTextNodes = TopicNode.SelectNodes("Mustertext");
            _Templates = new TextTemplate[templateTextNodes.Count];
            int a = 0;

            foreach (XmlNode curTextNode in templateTextNodes)
            {
                _Templates[a] = new TextTemplate(TemplateFilePath, curTextNode);
                a++;
            }
        }

        private void LoadAdditionalTopics()
        {
            var additionalTopicNodes = TopicNode.SelectNodes("Zusatzthema");
            _AdditionalTopics = new AdditionalProductTopic[additionalTopicNodes.Count];
            int a = 0;

            foreach (XmlNode curAdditionalTopicNode in additionalTopicNodes)
            {
                _AdditionalTopics[a] = new AdditionalProductTopic(TemplateFilePath, curAdditionalTopicNode);
                a++;
            }
        }
    }
}
